<template>
  <el-dialog :title="title"
             :visible.sync="dialogFormVisible">
    <el-form>
      <el-form-item label="商品的名称"
                    :label-width="formLabelWidth">
        <el-input v-model="goods.name"
                  autocomplete="off"
                  class="ipt"></el-input>
      </el-form-item>
      <el-form-item label="市场价格"
                    :label-width="formLabelWidth">
        <el-input v-model="goods.market_price"
                  autocomplete="off"
                  class="ipt"></el-input>
      </el-form-item>
      <el-form-item label="销售价格"
                    :label-width="formLabelWidth">
        <el-input v-model="goods.price"
                  autocomplete="off"
                  class="ipt"></el-input>
      </el-form-item>
      <el-form-item label="进价"
                    :label-width="formLabelWidth">
        <el-input v-model="goods.purchase_price"
                  autocomplete="off"
                  class="ipt"></el-input>
      </el-form-item>
      <el-form-item label="数量"
                    :label-width="formLabelWidth">
        <el-input v-model="goods.num"
                  autocomplete="off"
                  :disabled="fag"
                  class="ipt"></el-input>
      </el-form-item>
      <el-form-item label="商品的状态"
                    :label-width="formLabelWidth">
        <el-select v-model="goods.status"
                   placeholder="请选择商品的状态">
          <el-option label="上架"
                     value="active"></el-option>
          <el-option label="下架"
                     value="unactive"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品的描述"
                    :label-width="formLabelWidth">
        <el-input v-model="goods.make"
                  autocomplete="off"
                  class="ipt"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer"
         class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary"
                 @click="submit()">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      dialogFormVisible: false,
      goods: {
      },
      title: '添加商品',
      formLabelWidth: '120px',
      type: '',
      fag: false,
      updateId: 0,
    }
  },
  methods: {
    open (type, id) {
      this.type = type;
      if (type == 'insert') {
        this.goods = {};
        this.goods.status = 'unactive'
        this.title = '添加商品'
        this.fag = false;
      } else if (type == 'update') {
        this.title = '修改商品'
        this.updateId = id;
        this.fag = true;
        this.getById(id);
      }
      this.dialogFormVisible = true
    },
    submit () {
      if (this.type == 'insert') {
        this.insert();
      } else if (this.type == 'update') {
        this.update();
      }

    },
    insert () {
      this.axios.post('/goods/insert', this.goods, { isTip: true }).then(res => {
        if (res.data.code == '2000') {
          this.$parent.getGoodsList();
          this.dialogFormVisible = false;
        }
      })
    },
    update () {
      this.axios.post('/goods/updateById', this.goods, { isTip: true }).then(res => {
        if (res.data.code == '2000') {
          this.$parent.getGoodsList();
          this.dialogFormVisible = false;
        }
      })
    },
    getById (id) {
      this.axios.post('/goods/getById', { id: id }).then(res => {
        if (res.data.code == '2000') {
          this.goods = res.data.data[0]
        }
      })
    }

  }

}
</script>

<style lang="less" scoped>
.ipt {
  width: 65%;
}
</style>